package top.yongyan0624.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import top.yongyan0624.pojo.Dept;
import top.yongyan0624.pojo.Teacher;
import top.yongyan0624.service.DeptService;
import top.yongyan0624.service.TeacherService;

import java.util.List;

/**
 * @author Yongyan
 * @description: TODO
 * @date 2020/12/23
 **/

@Controller
@RequestMapping("/dept")
@SessionAttributes("error")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @RequestMapping("/toAddDeptPage")
    public String toAddDeptPage(Model model){
        model.addAttribute("error","");

        return "addDeptPage";
    }

    @RequestMapping("/addDept")
    public String addDept(Dept dept, Model model){
        Dept c = deptService.findById(dept.getDno());
        if (c == null){
            deptService.addDept(dept);
        } else {
            model.addAttribute("error","该系编号已被录入！");
            return "addDeptPage";
        }
        return "redirect:/dept/toFindAllDeptPage/1/5";
    }

    @RequestMapping("/deleteDept/{id}")
    public String deleteDept(@PathVariable("id") Integer id){
        deptService.deleteDept(id);
        return "redirect:/dept/toFindAllDeptPage/1/5";
    }

    @RequestMapping("/toUpdateDeptPage/{id}")
    public String toUpdateDeptPage(@PathVariable Integer id, Model model){
        Dept dept = deptService.findById(id);
        model.addAttribute("dept",dept);

        return "updateDeptPage";
    }

    @RequestMapping("/updateDept")
    public String updateDept(Dept dept){
        deptService.updateDept(dept);
        return "redirect:/dept/toFindAllDeptPage/1/5";
    }

    @RequestMapping("/toFindAllDeptPage/{pageNum}/{pageSize}")
    public String toFindAllPage(@PathVariable(value = "pageNum") Integer pageNum, @PathVariable(value = "pageSize") Integer pageSize,Model model){
        PageInfo<Dept> pageInfo = deptService.findAllDept(pageNum, pageSize);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("deptList",pageInfo.getList());
        model.addAttribute("error","");

        return "findAllDeptPage";
    }

    @RequestMapping("/findDept")
    public String findDept(Dept dept,Model model){
        List<Dept> deptList = deptService.findDept(dept);

        if (deptList.size()<=0){
            model.addAttribute("error","未找到该系...");

        } else if (dept.getDno() == null && dept.getName().equals("")) {
            model.addAttribute("error","");

        } else {
            model.addAttribute("deptList",deptList);
            model.addAttribute("error","");
            return "findAllDeptPage";
        }

        return "redirect:/dept/toFindAllDeptPage/1/5";

    }

}
